package siau.android.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.lifecycle.MutableLiveData
import hhm.android.base.activity.BaseActivity
import hhm.android.base.activity.DeviceUtils
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel

class MyFlutterActivity : FlutterActivity() {

    companion object {
        val statusBarStatus = MutableLiveData(-1)//-1一开始初始化  0  白底黑字 ；1  黑底白字 ；  2 沉浸式状态栏 白；  3 沉浸式状态栏 黑
    }

    private var channel: MyMethodChannel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        channel = MyMethodChannel.registerWith(this, flutterEngine?.dartExecutor)

        statusBarStatus.observe(this, {
            when (it) {
                0 -> {
                    setTitleWhiteAndTextBlank()
                }
                1 -> {
                    setTitleBlackAndTextWhite()
                }
                2 -> {
                    setMyTitleColor(0, isStatusColorTransform = true, whiteText = true)
                }
                3 -> {
                    setMyTitleColor(0, isStatusColorTransform = true, whiteText = false)
                }
            }
        })
    }

    private fun setTitleMainColorAndTextWhite() {
        setMyTitleColor(true)
    }

    private fun setTitleBlackAndTextWhite() {
        setMyTitleColor(Color.BLACK, true)
    }

    private fun setTitleWhiteAndTextBlank() {
        setMyTitleColor(Color.WHITE, false)
    }

    private fun setMyTitleColor(color: Int, whiteText: Boolean) {
        setMyTitleColor(color, false, whiteText)
    }

    private fun setMyTitleColor(whiteText: Boolean) {
        setMyTitleColor(BaseActivity.themeColor, false, whiteText)
    }

    /**
     * 沉浸式状态栏
     */
    private fun setMyTitleColor(intColor: Int, isStatusColorTransform: Boolean, whiteText: Boolean) {
        var color = intColor
        if (isStatusColorTransform) {
            color = 0
        }
        val decorView = window.decorView
        //状态栏文字黑色
        val options: Int = if (whiteText) {
            //状态栏文字白色
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        } else {
            //状态栏文字黑色
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        decorView.systemUiVisibility = options
        window.statusBarColor = color
    }

    override fun onDestroy() {
        channel?.release()
        channel = null
        super.onDestroy()
    }
}